package schoolbolg_gh326.fragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import schoolbolg_gh326.Activity.Detail;
import schoolbolg_gh326.Adapter.BlogsAdapter;
import schoolbolg_gh326.Bean.BlogsBean;
import schoolbolg_gh326.Bean.Constants;
import schoolbolg_gh326.service.BlogService;

import com.example.schoolbolg_gh326.R;
import com.example.schoolbolg_gh326.R.layout;

import android.app.Activity;
import android.content.Intent;
import android.net.NetworkInfo.DetailedState;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * A simple {@link Fragment} subclass. Activities that contain this fragment
 * must implement the {@link IndexFragment.OnFragmentInteractionListener}
 * interface to handle interaction events. Use the
 * {@link IndexFragment#newInstance} factory method to create an instance of
 * this fragment.
 *
 */
public class IndexFragment extends Fragment {
	private ListView lv;
	private View view;
	private Retrofit retrofit;
	private BlogService service;
	private Call<List<BlogsBean>> getlist;
	private List<BlogsBean> list;
	private BlogsAdapter adapter;
	 
	
	

	public IndexFragment() {
		// Required empty public constructor
		retrofit =new Retrofit
				.Builder()
				.baseUrl(Constants.web)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		
	}

	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view= inflater.inflate(R.layout.fragment_index, container,false);
		init();
		service = retrofit.create(BlogService.class);
		getlist = service.getlist(1, "blogitem", Constants.sessionID);
		getlist.enqueue(new Callback<List<BlogsBean>>() {
			
			@Override
			public void onResponse(Call<List<BlogsBean>> arg0,
					Response<List<BlogsBean>> arg1) {
				// TODO Auto-generated method stub
				if (arg1.isSuccessful()) {
					list = arg1.body();
					if (list !=null) {
						adapter = new BlogsAdapter(getActivity(), list);
						lv.setAdapter(adapter);
						lv.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {
								Intent intent = new Intent(getActivity(),Detail.class);
								intent.putExtra("title", list.get(position).getName());
								intent.putExtra("content", list.get(position).getContent());
								intent.putExtra("thumb", list.get(position).getThumb());
								startActivity(intent);
								
							}
						});
						
					}
					
				}
			}
			
			@Override
			public void onFailure(Call<List<BlogsBean>> arg0, Throwable arg1) {
				// TODO Auto-generated method stub
				
			}
		});
		// Inflate the layout for this fragment
		return view;
	}

	private void init() {
		// TODO Auto-generated method stub
		lv = (ListView) view.findViewById(R.id.blog_listView);
	}



	// TODO: Rename method, update argument and hook method into UI event

	
	

	public void refresh() {
		// TODO Auto-generated method stub
		
	}

}
